package service;

import bean.Student;
import dao.StudentDao;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

//用于判断
//调用dao层
public class StudenService {
private StudentDao studentDao;

    /**
     * 用户登录的业务类
     * request
     * username
     * password
     */
public boolean login(HttpServletRequest request, HttpServletResponse response, String username, String password){
    studentDao=new StudentDao();
//    封装对象
    Student student=new Student();
    student.setUsername(username);
    student.setPassword(password);
//调用dao层查询，登录
    boolean flag = null == studentDao.login(student) ? false : true;  //如果有返回true

      /*
      登录成功之后可调用的东西
        */

    //宿舍号
    String dormitoryid = studentDao.getDomitoryidByUsername(username);
    //根据宿舍号得到同寝室的人员
    ArrayList<Student> students = studentDao.getStudentsByDormintoryid(dormitoryid);
    //登录成功后获取姓名
    String studentname = studentDao.getStudentnameByUsername(username);
//保存session  存入session域
    if (flag){

//        request.getSession().setAttribute(student.getStudentid()+"",student);
        request.getSession().setAttribute("student",student);
        request.getSession().setAttribute("dormitoryid",dormitoryid);
        request.getSession().setAttribute("students",students);
        request.getSession().setAttribute("studentname",studentname);

    }else {
        System.out.println("登录失败");
    }
    return flag;
}

}
